<template>

  <div>

    <!--  查询-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">


        <el-form-item label="计算机名称">
          <el-input placeholder="请输入计算机名称" v-model.trim="queryParam.name" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="IP地址">
          <el-input placeholder="请输入IP地址" v-model.trim="queryParam.ips" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="云主机类型" prop="cloudType">
          <el-select v-model="queryParam.cloudType" placeholder="请选择" clearable>
            <el-option
              v-for="item in cloudTypeOpt"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否虚拟机" prop="vm">
          <el-radio v-model="queryParam.vm" :label="1" >虚拟机</el-radio>
          <el-radio v-model="queryParam.vm" :label="0" >物理机</el-radio>
        </el-form-item>
        <el-form-item label="场所名称">
          <el-input placeholder="请输入场所名称" v-model.trim="queryParam.siteName" clearable size="mini"></el-input>
        </el-form-item>



        <el-form-item>
          <el-button type="success" size="mini" @click="getListPage">查询</el-button>
          <el-button type="info" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-form>

    </el-card>





    <!--列表-->
    <el-card style="margin-top: 10px">

      <el-table
        :data="tableData"
        max-height="600px"
        height="600px"
        style="width: 100%">


        <el-table-column prop="siteName" label="现场名称">
          <template slot-scope="scope">
            <el-link type="primary" :href="getSiteUrl(scope.row.siteId)" target="_blank">
              {{ scope.row.siteName }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="计算机名称"/>
        <el-table-column prop="parentName" label="父级名称"/>
        <el-table-column prop="ips" label="IP"/>

        <el-table-column prop="osTypeName" label="系统类型"/>
        <el-table-column prop="specialName" label="特殊类型"/>
        <el-table-column prop="useStatus" label="状态">
          <template slot-scope="scope">
            <el-tag type="success" size="mini" v-if="scope.row.useStatus === 1">使用</el-tag>
            <el-tag type="info" size="mini" v-else-if="scope.row.useStatus === 9">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="vm" label="是否虚拟机">
          <template slot-scope="scope">
            <el-tag type="danger" size="mini" v-if="scope.row.vm === 0">物理机</el-tag>
            <el-tag type="success" size="mini" v-else-if="scope.row.vm === 1">虚拟机</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="cloudTypeName" label="云主机类型"/>



        <el-table-column fixed="right" label="操作" width="60">
          <template slot-scope="scope">
            <el-button @click="openViewBefore(scope.row)" type="text" size="small"><i class="el-icon-view"></i>查看</el-button>
          </template>
        </el-table-column>

      </el-table>


      <!--分页部分-->
      <el-pagination
        style="width:33%; margin-right:10px; margin-top:10px;"
        background
        layout="total, prev, pager, next, jumper"
        @current-change="pageCurrentChange"
        :page-size="pageConfig.size"
        :current-page="pageConfig.current"
        :pager-count="5"
        :total="pageConfig.total">
      </el-pagination>

    </el-card>

    <ComputerDetailed ref="ComputerDetailedRef"/>

  </div>

</template>

<script>

import http from '@/utils/http'
import ComputerDetailed from "./ComputerDetailed.vue";

export default {
  name: "ComputerList",
  components: {
    ComputerDetailed
  },
  data() {
    return {
      // 查询参数
      queryParam: {
        name: null,
        ips: null,
        cloudType: null,
        vm: null,
        siteName: null,
      },
      // 分页参数
      pageConfig: {
        // 每页记录数
        size: 20,
        // 当前页码
        current: 1,
        // 总记录数
        total: 0,
      },
      // 表格数据
      tableData: [],
      // 云主机类型
      cloudTypeOpt: [
        {code: '', name: ''},
      ],
    }
  },
  methods: {
    /**
     * 查看之前触发
     * @param obj 这一行数据
     */
    openViewBefore(obj){
      this.$refs.ComputerDetailedRef.init(obj);
    },

    /**
     * 获取云主机类型
     */
    getCloudType(){
      http.post("/computer/getCloudType", null).then(response => {
        let res = response.data;
        this.cloudTypeOpt = res.data

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 分支分页查询列表
     */
    getListPage() {
      this.queryParam.current = this.pageConfig.current
      this.queryParam.size = this.pageConfig.size
      http.post("/computer/getAllComputerServerPageList", this.queryParam).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        this.pageConfig.total = data.total;
        this.pageConfig.current = data.current;
        this.pageConfig.size = data.size;
        // 表格数据
        this.tableData = data.records

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 分页被改变的时候
     * @param value 当前页
     */
    pageCurrentChange(value) {
      this.pageConfig.current = value
      this.getListPage()
    },
    /**
     * 重置查询条件
     */
    resetQuery() {
      this.queryParam ={
        name: null,
        ips: null,
        cloudType: null,
        vm: null,
        siteName: null,
      }
    },
    /**
     * 分页前重置分页参数
     */
    getPageListBefore() {
      this.pageConfig.current = 1
      this.getListPage()
    },
    /**
     * 跳转到场所计算机页面
     * @param siteId 场所id
     * @returns {string}
     */
    getSiteUrl(siteId) {
      // 生成完整URL（需要确保路由配置正确）
      return `${window.location.origin}/#/toComputerServerList?siteId=${siteId}`;
      // 或者如果你的路由是history模式
      // return `${window.location.origin}/toSiteLayout?siteId=${siteId}`;
    },
  },
  // 进入马上执行
  mounted() {
    this.getListPage()
    this.getCloudType()
    this.$store.commit ('navigationListChange', [
      {name: '全局计算机', url: '/toComputerList', query: {}},
    ])
  },
}
</script>



<style scoped>

</style>
